<template>
    <div class="forgetpwd" style="margin-top:5vh; margin-left: 1vh; margin-right: 1vh;">
        <van-cell-group>
            <van-field v-model="username" required clearable name="username" label="用户名" />
            <van-row>
                <van-col span="16"><van-field v-model="mail" required clearable9 label="验证码"></van-field></van-col>
                <van-col span="8"><van-button type="info" :disabled="wait_type" @click="sendSMS">{{ wait_text }}</van-button></van-col>
            </van-row>
            <van-field v-model="newpassword" type="password" required clearable label="新密码" placeholder="请输入新密码" />
            <van-field v-model="confirmpassword" type="password" required clearable label="确认密码" placeholder="请再输入一次密码" />
        </van-cell-group>
        <van-button :disabled="change_success" type="info" size="large" style="margin-top:5vh;" @click="forgetpwd">{{ change_text }}</van-button>
    </div>
</template>

<script>
import Vue from 'vue'
import { Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, Col, Row } from 'vant'
import qs from 'qs'
// import { setInterval } from 'timers'
Vue.use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(Col).use(Row)

export default {
    name: 'forgetpwd',
    data () {
        return {
            username: this.$route.params.username,
            mail: '',
            mail_back: '',

            newpassword: '',
            confirmpassword: '',

            wait_text: '发送验证码',
            wait_second: 10,
            wait_type: false,
            wait_timer: null,

            change_success: false,
            change_text: '确认'
        }
    },
    created: function () {

    },
    methods: {
        sendSMS: function () {
            if (!this.username) {
                Toast('请输入用户名')
            } else {
                //console.log('click')
                this.wait_type = true
                this.wait_timer = window.setInterval(this.timeFuc, 1000)

                let data = {
                    username: this.username,
                    wxuserid: this.wxuserid,
                    dduserid: this.dduserid,
                    wxgzuserid: this.wxgzuserid,
                    clienttype: 'mobile'
                }
                this.axios({
                    method: 'post',
                    url: '/api/loginuser.verification.do',
                    data: qs.stringify(data)
                }).then(res => {
                    if (!res.data.message) {
                        Toast(res.data.des)
                        if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                            this.loginout()
                        }
                    } else {
                        Toast(res.data.message)
                    }
                })
            }
        },
        timeFuc: function () {
            this.wait_second--
            if (this.wait_second >= 0) {
                // this.wait_type = true;
                this.wait_text = this.wait_second + 's后重新发送'
            } else {
                this.wait_type = false
                this.wait_text = '重新发送验证码'
                window.clearTimeout(this.wait_timer)
                this.wait_second = 5
            }
        },
        forgetpwd: function () {
            if (!this.username) { Toast('请输入用户名') } else if (!this.mail) { Toast('请输入验证码') } else if (!this.newpassword) { Toast('请输入新密码') } else if (!this.confirmpassword) { Toast('请确认新密码') } else if (!this.newpassword === this.confirmpassword) { Toast('两次密码不一致') } else {
                let data = {
                    user: this.username,
                    yzm: this.mail,
                    newmm: this.newpassword,
                    qrnewmm: this.confirmpassword
                }

                this.axios({
                    method: 'post',
                    url: '/api/login.changepwd.do',
                    data: qs.stringify(data)
                }).then(res => {
                    //console.log(res.data)
                    if (!res.data.message) {
                        Toast(res.data.des)
                        if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                            this.loginout()
                        }
                    } else {
                        if (res.data.code === 200) {
                            this.change_success = true
                            this.change_text = '修改成功，3s后返回上页'
                            setTimeout(() => {
                                this.$router.go(-1)
                            }, 3000)
                        } else {
                            Toast(res.data.message)
                        }
                    }
                })
            }
        }
    }
}
</script>
